package com.bjb.vr.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjb.vr.common.ao.OptClassifyAo;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.OptClassifyConfig;
import com.bjb.vr.common.dto.ResDictionary;
import com.bjb.vr.common.mapper.OptClassifyConfigMapper;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.OptClassifyVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author admin
 * @description 针对表【opt_classify_config】的数据库操作Service实现
 * @createDate 2022-12-13 17:13:22
 */
@Service
public class OptClassifyConfigService extends ServiceImpl<OptClassifyConfigMapper, OptClassifyConfig> {

    @Resource
    private OptClassifyConfigMapper classifyConfigMapper;

    @Resource
    private ResDictionaryService dictionaryService;


    public List<OptClassifyVo> queryClassifyList(OptClassifyAo optClassifyAo) {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        List<OptClassifyVo> optClassifyVos = new ArrayList<>();
        LambdaQueryWrapper<OptClassifyConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(OptClassifyConfig::getOrderNum);
        List<OptClassifyConfig> optClassifyConfigs = classifyConfigMapper.selectList(queryWrapper);
        optClassifyConfigs.stream().forEach(p -> {
            OptClassifyVo optClassifyVo = new OptClassifyVo();
            BeanUtils.copyProperties(p, optClassifyVo);
            optClassifyVos.add(optClassifyVo);
        });
        return optClassifyVos;
    }


    public OptClassifyVo queryClassifyOne(OptClassifyAo optClassifyAo) {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        OptClassifyConfig optClassifyConfig = classifyConfigMapper.selectById(optClassifyAo.getId());
        OptClassifyVo optClassifyVo = new OptClassifyVo();
        BeanUtils.copyProperties(optClassifyConfig, optClassifyVo);
        if (StringUtils.isNotBlank(optClassifyVo.getPeriod())) {
            optClassifyVo.setPeriodCode(dictionaryService.getDictionaryByName(optClassifyVo.getPeriod()).getDictCode());
        }
        if (StringUtils.isNotBlank(optClassifyVo.getSubject())) {
            optClassifyVo.setSubjectCode(dictionaryService.getDictionaryByName(optClassifyVo.getSubject()).getDictCode());
        }
        if (StringUtils.isNotBlank(optClassifyVo.getClassClassify())) {
            optClassifyVo.setClassClassifyCode(dictionaryService.getDictionaryByName(optClassifyVo.getClassClassify()).getDictCode());
        }
        if (StringUtils.isNotBlank(optClassifyVo.getThemeName())) {
            optClassifyVo.setThemeCode(dictionaryService.getDictionaryByName(optClassifyVo.getThemeName()).getDictCode());
        }
        return optClassifyVo;
    }

    public BaseResult addClassify(OptClassifyAo optClassifyAo) {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        // 判断是否超限制
        List<OptClassifyConfig> classifyConfigs = classifyConfigMapper.selectList(new LambdaQueryWrapper<>());
        if (classifyConfigs.size() >= 3) {
            log.error("分类数量超过限制【3个】");
            return CommonResult.Fail("分类数量超过限制【3个】");
        }

        OptClassifyConfig optClassifyConfig = new OptClassifyConfig();
        BeanUtils.copyProperties(optClassifyAo, optClassifyConfig);
        try {
            // 设置资源的属性
            if (classifyConfigs.size() > 0) {
                Integer orderNum = classifyConfigs.stream().max(Comparator.comparing(OptClassifyConfig::getOrderNum)).get().getOrderNum();
                optClassifyConfig.setOrderNum(orderNum + 1);
            }
            exchangeDictValue(optClassifyAo, optClassifyConfig);
            classifyConfigMapper.insert(optClassifyConfig);
            return CommonResult.Ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.Fail();
        }
    }

    /**
     * 设置字典值
     *
     * @param optClassifyAo
     * @param classifyConfig
     */
    private void exchangeDictValue(OptClassifyAo optClassifyAo, OptClassifyConfig classifyConfig) {
        // 清空数据
        classifyConfig.setThemeName(null);
        classifyConfig.setPeriod(null);
        classifyConfig.setSubject(null);
        classifyConfig.setClassClassify(null);
        // 封装数据
        String periodCode = optClassifyAo.getPeriod();
        if (StringUtils.isNotBlank(periodCode)) {
            ResDictionary dictionary = dictionaryService.getResDictionaryByCode(periodCode);
            classifyConfig.setPeriod(dictionary.getDictName());
        }
        String subjectCode = optClassifyAo.getSubject();
        if (StringUtils.isNotBlank(periodCode)) {
            ResDictionary dictionary = dictionaryService.getResDictionaryByCode(subjectCode);
            classifyConfig.setSubject(dictionary.getDictName());
        }
        String themeCode = optClassifyAo.getThemeCode();
        if (StringUtils.isNotBlank(themeCode)) {
            ResDictionary dictionary = dictionaryService.getResDictionaryByCode(themeCode);
            classifyConfig.setThemeName(dictionary.getDictName());
        }
        String classClassify = optClassifyAo.getClassClassify();
        if (StringUtils.isNotBlank(classClassify)) {
            ResDictionary dictionary = dictionaryService.getResDictionaryByCode(classClassify);
            classifyConfig.setClassClassify(dictionary.getDictName());
        }
    }

    public BaseResult editClassify(OptClassifyAo optClassifyAo) {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        OptClassifyConfig optClassifyConfig = new OptClassifyConfig();
        BeanUtils.copyProperties(optClassifyAo, optClassifyConfig);
        try {
            if (!optClassifyAo.getChangeOrder()) {
                exchangeDictValue(optClassifyAo, optClassifyConfig);
                classifyConfigMapper.updateById(optClassifyConfig);
                return CommonResult.Ok();
            } else {
                Integer originOrder = null;
                Integer targetOrder = null;
                OptClassifyConfig originObject = classifyConfigMapper.selectById(optClassifyAo.getOriginId());
                OptClassifyConfig targetObject = classifyConfigMapper.selectById(optClassifyAo.getTargetId());
                originOrder = originObject.getOrderNum();
                targetOrder = targetObject.getOrderNum();
                originObject.setOrderNum(targetOrder);
                targetObject.setOrderNum(originOrder);
                classifyConfigMapper.updateById(originObject);
                classifyConfigMapper.updateById(targetObject);
                return CommonResult.Ok();
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.Fail();
        }
    }

    public BaseResult deleteClassify(OptClassifyAo optClassifyAo) {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        try {
            classifyConfigMapper.deleteById(optClassifyAo.getId());
            return CommonResult.Ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.Fail();
        }
    }
}




